﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using HRWEB.Models;

namespace HRWEB.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Message = "青岛大学医学院附属医院人事信息系统";
            ViewBag.Title = "青医附院人力资源管理系统";
            EmployeeModel empModel = new EmployeeModel(User.Identity.Name);
            HttpContext.Profile["ID"] = empModel.Employee.FEmployeeID;
            return View(empModel);
        }
        [HttpPost]
        public ActionResult PhotoUpload()
        {
            HttpPostedFileBase image = Request.Files[0];
            if (image.ContentType == "image/jpeg")
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    TEmployee t = ent.TEmployees.Where(p => p.FPublicID == User.Identity.Name).Single();
                    t.FPhoto.Data = new byte[image.ContentLength];
                    image.InputStream.Read(t.FPhoto.Data, 0, image.ContentLength);
                    t.FPhoto.MIME = image.ContentType;
                    ent.SaveChanges();
                }
                JsonResult j = Json(new { message = "照片上传成功" });
                j.ContentType = "text/plain";
                return j;
            }
            JsonResult jr = Json(new { message = "请上传JPEG格式的照片" });
            jr.ContentType = "text/plain";
            return jr;
        }
        [HttpPost]
        public ActionResult Index(EmployeeModel em,HttpPostedFileBase image)
        {
            EmployeeModel model = new EmployeeModel(User.Identity.Name);
            //if (image != null)
            //{
            //    if (image.ContentType == "image/jpeg")
            //    {
            //        model.Employee.FPhoto = new byte[image.ContentLength];
            //        image.InputStream.Read(model.Employee.FPhoto, 0, image.ContentLength);
            //        model.Employee.FPhotoMIME = image.ContentType;
            //        model.Save();
            //        //using (hrdbEntities ent = new hrdbEntities())
            //        //{
            //        //    TEmployee t = ent.TEmployees.Where(p => p.FPublicID == User.Identity.Name).Single();
            //        //    t.FPhoto = new byte[image.ContentLength];
            //        //    image.InputStream.Read(t.FPhoto, 0, image.ContentLength);
            //        //    t.FPhotoMIME = image.ContentType;
            //        //    ent.SaveChanges();
            //        //}
            //    }
            //    else
            //    {
            //        ModelState.AddModelError("WrongPhotoFormat", "请上传jpeg格式的照片");
            //    }
            //}
            if (string.IsNullOrEmpty(em.Employee.FBornCity) || string.IsNullOrEmpty(em.Employee.FMobileTel) || string.IsNullOrEmpty(em.Employee.FAdress))
            {
                ModelState.AddModelError("FillAll", "请将空白信息填写完整");
            }
            if (model.Employee.FPhoto == null)
            {
                ModelState.AddModelError("NeedPhoto", "请上传您的照片");
            }
            model.Employee.FBornCity = em.Employee.FBornCity;
            model.Employee.FMarriageCode = em.Employee.FMarriageCode;
            model.Employee.FMobileTel = em.Employee.FMobileTel;
            model.Employee.FAdress = em.Employee.FAdress;
            model.Save();
            TempData["Message"] = "已经成功保存更改";
            if (ModelState.IsValid)
            {
                
            }
            return RedirectToAction("Index","Home");
        }
        public FileContentResult GetPhoto()
        {
            EmployeeModel model = new EmployeeModel(User.Identity.Name);
            if (model.Photo != null)
            {
                if (model.PhotoMIME == null || model.PhotoMIME == "") { model.PhotoMIME = "image/jpeg"; }
                return File(model.Photo, model.PhotoMIME);
            }
            else
            {
                return null;
            }
        }
        public ActionResult About()
        {
            return View();
        }
    }
}
